<template>
</template>

<script>
	let that;
	let app = getApp();
	export default {
		onLoad: function(options) {
			that = this;
			//平台切换
			let platForm = app.globalData.platForm || uni.getSystemInfoSync().uniPlatform;
			app.globalData.platForm = platForm;
			switch (platForm) {
				case 'mp-toutiao': { //抖音
					that.toutiao();
					break;
				}
				case 'mp-weixin': {
					that.weixin(options);
					break;
				};
				default: {
					uni.showModal({
						title: '提示',
						content: '数据加载失败，请刷新小程序',
						success: function (res) {
							if (res.confirm) {
								uni.reLaunch({
									url: '/pages/index/index'
								})
							} else if (res.cancel) {
								console.log('用户点击取消');
								uni.reLaunch({
									url: '/pages/index/index'
								})
							}
						}
					});
				}
			}

			uni.showShareMenu({
				withShareTicket: true,
				menu: ['shareAppMessage', 'shareTimeline']
			})


		},

		// 分享页面
		onShareAppMessage(res) {
			if (app.globalData.cityInfo.name) {
				var cityname = app.globalData.cityInfo.name;
			} else {
				var cityname = '';
			}
			var title = app.globalData.appConfig.share.title;
			title = title.split('$city').join(cityname);
			if (app.globalData.userid) {
				var useridShare = '?fromShare=' + app.globalData.userid;
			} else {
				var useridShare = '';
			}
			return {
				title: title,
			};
		},

		methods: {
			toutiao() { //抖音配置、首页加载
				uni.showLoading({
					title: '加载中...',
				});
				let dyConfig = app.globalData.dyConfig;
				let siteConfig = app.globalData.siteConfig; //表示已经获取到值 因为 miniProgramTemplate可能为空
				if (dyConfig || (siteConfig && siteConfig.baseHost)) {
					redirectPage(dyConfig);
				} else {
					app.watchG('dyConfig', function(value) {
						redirectPage(value);
					})
				}

				function redirectPage(value) {
					let dyConfig = value;
					let direct_path = uni.getExtConfigSync && uni.getExtConfigSync()['path'] ?
					uni.getExtConfigSync()['path'] : '';    //此处为了兼容但分站打包
					if(direct_path){
						uni.reLaunch({
							url: direct_path,
							fail: res => {
								uni.showModal({
									title: '页面出错',
									content: '要显示的页面不存在'
								})
							}
						});
						return false;
					}	
					if (Object.keys(dyConfig).length !== 0) {
						if (dyConfig != '' && dyConfig.template != '' && dyConfig.template != undefined) {
							//模块原生页面
							if (dyConfig.template.indexOf('skin') > -1 ) {
								uni.reLaunch({
									url: '/pages/' + dyConfig.template + '/index',
									fail: res => {
										uni.showModal({
											title: '页面出错',
											content: '要显示的页面不存在'
										})
									}
								})
							}else if(dyConfig.template == 'diy'){
								uni.reLaunch({
									url: '/pages/packages/' + dyConfig.template + '/index',
									fail: res => {
										uni.showModal({
											title: '页面出错',
											content: '要显示的页面不存在'
										})
									}
								})
							} else {
								uni.reLaunch({
									url: '/pages/packages/' + dyConfig.template + '/index/index',
									fail: res => {
										uni.showModal({
											title: '页面出错',
											content: '要显示的页面不存在'
										})
									}
								})
							}
						} else {
							let url = app.globalData.masterDomain + '?ByteMiniProgram=1'
							uni.reLaunch({
								url: '/pages/redirect/index?url=' + encodeURIComponent(app.addUrlParam(
									url))
							})
						}
					} else {
						//请求失败的情况，直接加载H5首页
						let url = app.globalData.masterDomain + '?ByteMiniProgram=1'
						uni.reLaunch({
							url: '/pages/redirect/index?url=' + encodeURIComponent(app.addUrlParam(
								url))
						})
					}
				}
				// 显示转发选项
				uni.showShareMenu({
					withShareTicket: true
				})
			},
			weixin(options) { //微信配置、首页加载
				uni.showLoading({
					title: '加载中...'
				});
				app.watchG('siteConfig', function(value) {
					redirectPage(value.miniProgramTemplate);
				});

				function redirectPage(value) {

					// uni.navigateTo({
					// 	url: '/pages/packages/company/index/index?uid=4&access_token=VW50WElGSjVBV2hWWmdBNFh6bFNQZ2N3Vm5oYmFndy9EMmhkTFFGekFIcFdmVmM1VkRoYmJnQnJWMzRFZGxVOVVYSlhjMVUyV2pFS01sMDlWREVIT0ZJMVYyWlNZQUZv&refresh_token=VkgwQWR3WXRCRzBETUZGcEFHWURiMVJqVUg0R04xdG9EMmhVSkZzcFZ5MVZmZ2RwV2pZRU1RVnVBaXRUSVFSc1ZuVlZjVkF6WERjUE4xMC9CR3NHTzFRK0FETUdOQVJ0&cityid=166&wxMiniProgramLoginByToken=1'
					// });

					// return false;



					let direct_path = uni.getExtConfigSync && uni.getExtConfigSync()['path'] ?
					uni.getExtConfigSync()['path'] : '';    //此处为了兼容但分站打包
					if(direct_path){
						uni.reLaunch({
							url: direct_path,
							fail: res => {
								// uni.showModal({
								// 	title: '页面出错',
								// 	content: '要显示的页面不存在'
								// })
							}
						});
						return false;
					}	
					if (value) {
						//大首页原生页面，风格一
						if (value == 'skin1') {
							uni.reLaunch({
								url: '/pages/skin1/index'
							});
						} else if (value == 'skin2') {
							uni.reLaunch({
								url: '/pages/skin2/index'
							});
						} else if (value == 'diy') {
							uni.reLaunch({
								url: '/pages/packages/diy/index'
							});
						} else {
							//模块原生页面
							uni.reLaunch({
								url: '/pages/packages/' + value + '/index/index'
							});
						}
					} else {
						let url = app.globalData.masterDomain + '?miniprogram=1';
						uni.reLaunch({
							url: '/pages/redirect/index?url=' + encodeURIComponent(app.addUrlParam(url))
						})
					}
				}
			}
		}
	}
</script>